//
//  WLBaseButton.m
//  Wallet
//
//  Created by swifter on 2017/12/29.
//  Copyright © 2017年 SwiftPass. All rights reserved.
//

#import "WLBaseButton.h"

@interface WLBaseButton ()
//@property (nonatomic, copy) ButtonEventCallBack eventCallback;
@end

@implementation WLBaseButton

static ButtonEventCallBack _eventCallback_;

- (void)dealloc {
    _eventCallback_ = nil;
}

#pragma mark init

- (instancetype)initWithFrame:(CGRect)frame eventCallback:(ButtonEventCallBack)callback  {
    self = [super initWithFrame:frame];
    if (self) {
        _eventCallback = callback;
    }
    return self;
}

+ (instancetype)buttonWithType:(UIButtonType)buttonType eventCallback:(ButtonEventCallBack)callback {
    WLBaseButton *btn = [WLBaseButton buttonWithType:buttonType];
    if (self) {
        _eventCallback_ = callback;
    }
    return btn;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addTarget:self action:@selector(btnPressed:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return self;
}

#pragma mark override

- (void)didMoveToSuperview {
    [super didMoveToSuperview];
    [self layoutIfNeeded];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self setupStyle];
}

- (CGSize)intrinsicContentSize {
    return (CGSize){304.0, 44};
}

- (void)setUserInteractionEnabled:(BOOL)userInteractionEnabled {
    [super setUserInteractionEnabled:userInteractionEnabled];
    if (!userInteractionEnabled) {
        // 不可用
//        [self setBackgroundImage:[self backgroundImageForState:(UIControlStateDisabled)]?:@"buttom_next_unavailable".image forState:(UIControlStateDisabled)];
        
         [self setBackgroundColor:kTextLightRed];
    }
    else {
        [self setBackgroundColor:kMainColor];

//        [self setBackgroundImage: [self backgroundImageForState:(UIControlStateNormal)] ?:@"buttom_next_default".image forState:(UIControlStateNormal)];
    }
}

- (void)setEnabled:(BOOL)enabled {
    [super setEnabled:enabled];
    if (!enabled) {
//        [self setBackgroundImage:[self backgroundImageForState:(UIControlStateDisabled)]?:@"buttom_next_unavailable".image forState:(UIControlStateDisabled)];
        // 不可用
        [self setBackgroundColor:kTextLightRed];

    }
    else {
//        [self setBackgroundImage: [self backgroundImageForState:(UIControlStateNormal)] ?:@"buttom_next_default".image forState:(UIControlStateNormal)];
        [self setBackgroundColor:kMainColor];
    }
}

#pragma mark setter

- (void)setNormalColor:(UIColor *)normalColor {
    _normalColor = normalColor;
    [self setBackgroundColor:normalColor];
}

- (void)setBgColor:(UIColor *)bgColor {
    _bgColor = bgColor;
    [self setBackgroundColor:bgColor];
}

- (void)setSelectedColor:(UIColor *)selectedColor {
    _selectedColor = selectedColor;
}

- (void)setHighlightColr:(UIColor *)highlightColr {
    _highlightColr = highlightColr;
}

- (void)setNormalTitleColor:(UIColor *)normalTitleColor {
    _normalTitleColor = normalTitleColor;
    [self setTitleColor:normalTitleColor forState:UIControlStateNormal];
}

- (void)setSelectedTitleColor:(UIColor *)selectedTitleColor {
    _selectedTitleColor = selectedTitleColor;
    [self setTitleColor:selectedTitleColor forState:UIControlStateSelected];
}

- (void)setHighlightTitleColr:(UIColor *)highlightTitleColr {
    _highlightTitleColr = highlightTitleColr;
    [self setTitleColor:highlightTitleColr forState:UIControlStateHighlighted];
}

- (void)setNormalTitle:(NSString *)normalTitle {
    _normalTitle = normalTitle;
    [self setTitle:normalTitle forState:(UIControlStateNormal)];
}

- (void)setSelectedTitle:(NSString *)selectedTitle {
    _selectedTitle = selectedTitle;
    [self setTitle:selectedTitle forState:UIControlStateSelected];
}

- (void)setHighlightTitle:(NSString *)highlightTitle {
    _highlightTitle = highlightTitle;
    [self setTitle:highlightTitle forState:UIControlStateHighlighted];
}

- (void)setFont:(UIFont *)font {
    _font = font;
    [self.titleLabel setFont:font];
}

- (void)setFontSize:(CGFloat)fontSize {
    _fontSize = fontSize;
    [self setFont:[UIFont systemFontOfSize:fontSize]];
}


+ (void)setEventCallback_:(ButtonEventCallBack)eventCallback_ {
    _eventCallback_ = eventCallback_;
}

+ (ButtonEventCallBack)eventCallback_ {
    if (!_eventCallback_) {
        _eventCallback_ = ^{};
    }
    return _eventCallback_;
}


#pragma mark public method

- (void)setFont:(UIFont *)font title:(NSString *)title titleColor:(UIColor *)color forState:(UIControlState)state {
    self.font = font;
    color = color ?: [UIColor whiteColor];
    font = font ?: [UIFont systemFontOfSize:[UIFont systemFontSize]];
    [self setTitleColor:color forState:state];
    [self setTitle:title forState:state];
}

- (void)setFontSize:(CGFloat)fontSize title:(NSString *)title titleColor:(UIColor *)color forState:(UIControlState)state {
    fontSize = fontSize ?: [UIFont systemFontSize];
    self.fontSize = fontSize;
    [self setFont:[UIFont systemFontOfSize:fontSize] title:title titleColor:color forState:state];
}

- (void)setFont:(UIFont *)font title:(NSString *)title titleColor:(UIColor *)color forState:(UIControlState)state backgroundColor:(UIColor *)bgColor {
    bgColor = bgColor ?: [UIColor clearColor];
    switch (state) {
        case UIControlStateNormal:
            [self setBgColor:bgColor];
            break;
        case UIControlStateSelected:
            [self setBgColor:bgColor];
            break;
        case UIControlStateHighlighted:
            [self setBgColor:bgColor];
            break;
        case UIControlStateDisabled:
            [self setBgColor:bgColor];
            break;
        default:
            [self setBgColor:bgColor];
            break;
    }
    [self setFont:font title:title titleColor:color forState:state];
}

- (void)setFontSize:(CGFloat)fontSize title:(NSString *)title titleColor:(UIColor *)color forState:(UIControlState)state backgroundColor:(UIColor *)bgColor {
    fontSize = fontSize ?: [UIFont systemFontSize];
    self.fontSize = fontSize;
    [self setFont:[UIFont systemFontOfSize:fontSize] title:title titleColor:color forState:state backgroundColor:bgColor];
}


#pragma mark private method

- (void)setupStyle {
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = MIN(self.bounds.size.height, self.bounds.size.width)/2;
}

- (void)btnPressed:(UIButton*)btn {
    if (self.eventCallback) {
        self.eventCallback();
    }
    else if (_eventCallback_) {
        _eventCallback_();
    }
}

- (void)buttonEnableBaseOnCurrentTextFile:(UITextField *)currentTextFile
                             textFiledOne:(UITextField *)textFiledOne
                             textfiledTwo:(UITextField *)textfiledTwo
                           textfiledThree:(UITextField *)textFiledThree
                            currentString:(NSString *)currentString
                              lengthLimit:(NSInteger)lengthLimit {
    if (currentTextFile == textFiledOne) {
        
    }
}

@end














